package com.testplatfrom.project.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.testplatfrom.common.core.exception.project.ProjectException;
import com.testplatfrom.common.security.utils.SecurityUtils;
import com.testplatfrom.project.domain.TestplatfromHttpconfiguration;
import com.testplatfrom.project.enumproject.environment.EnablingConditions;
import com.testplatfrom.project.enumproject.environment.HttpRoutePatchMethod;
import com.testplatfrom.project.mapper.TestplatfromHttpconfigurationMapper;
import com.testplatfrom.project.service.TestplatfromHttpconfigurationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * http环境配置 服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2023-08-07
 */
@Service
public class TestplatfromHttpconfigurationServiceImpl extends ServiceImpl<TestplatfromHttpconfigurationMapper, TestplatfromHttpconfiguration> implements TestplatfromHttpconfigurationService {

    /**
     * 新增或者修改
     * @param testplatfromHttpconfigurationList HTTP配置list
     * @param environmentId 若是新增 则需要传环境id
     * @param bool 新增为true update 为false
     */
    @Override
    public void HttpConfigCheck(List<TestplatfromHttpconfiguration> testplatfromHttpconfigurationList, String environmentId, Boolean bool) {
        //HTTP配置
        if (!CollectionUtils.isEmpty(testplatfromHttpconfigurationList)){
            //HTTP配置列表组装
            for (TestplatfromHttpconfiguration testplatfromHttpconfiguration : testplatfromHttpconfigurationList) {
                //启用条件转换
                EnablingConditions enablingConditions = EnablingConditions.getByKey(testplatfromHttpconfiguration.getHttpEbabling());
                if (ObjectUtil.isNull(enablingConditions)){
                    throw new ProjectException("启用条件类型错误");
                }
                testplatfromHttpconfiguration.setHttpEbabling(enablingConditions.name());
                //若为路径匹配方式
                if (enablingConditions.getHttpEbablingMethod().equals("ROUTE") && ObjectUtil.isNotNull(testplatfromHttpconfiguration.getHttpRoutePatch())){
                    HttpRoutePatchMethod httpRoutePatchMethod = HttpRoutePatchMethod.getByValue(testplatfromHttpconfiguration.getHttpRoutePatch());
                    if (ObjectUtil.isNull(httpRoutePatchMethod)){
                        throw new ProjectException("路径匹配方式错误");
                    }
                    if (ObjectUtil.isEmpty(testplatfromHttpconfiguration.getHttpRouteValue())){
                        throw new ProjectException("路径匹配值不能为空");
                    }
                    testplatfromHttpconfiguration.setHttpRoutePatch(httpRoutePatchMethod.name());
                //若为模块
                } else if (enablingConditions.getHttpEbablingMethod().equals("MODULE")){
                    if (ObjectUtil.isEmpty(testplatfromHttpconfiguration.getModulePatch())){
                        throw new ProjectException("模块设置列表不难为空");
                    }
                }
            }
            //若是修改 先移除再新增
            if (!bool){
                LambdaQueryWrapper<TestplatfromHttpconfiguration> testplatfromHttpconfigurationLambdaQueryWrapper=new LambdaQueryWrapper<>();
                testplatfromHttpconfigurationLambdaQueryWrapper.eq(TestplatfromHttpconfiguration::getEnvironmentId, environmentId);
                this.remove(testplatfromHttpconfigurationLambdaQueryWrapper);
            }
            //赋值环境id
            for (TestplatfromHttpconfiguration testplatfromHttpconfiguration : testplatfromHttpconfigurationList) {
                testplatfromHttpconfiguration.setEnvironmentId(environmentId);
                testplatfromHttpconfiguration.setCreateBy(SecurityUtils.getUsername());
                testplatfromHttpconfiguration.setCreateTime(LocalDateTime.now());
            }
            //保存
            this.saveBatch(testplatfromHttpconfigurationList);
        }
    }
    /***
     * 根据环境id获取详情
     * @param environmentId 环境id
     * @return
     */
    @Override
    public List<TestplatfromHttpconfiguration> getHttpConfigDetail(String environmentId) {
        List<TestplatfromHttpconfiguration> testplatfromHttpconfigurations = list(new LambdaQueryWrapper<TestplatfromHttpconfiguration>()
                .eq(TestplatfromHttpconfiguration::getEnvironmentId, environmentId));
        if (CollectionUtils.isEmpty(testplatfromHttpconfigurations)){
            return null;
        }
        for (TestplatfromHttpconfiguration testplatfromHttpconfiguration : testplatfromHttpconfigurations) {
            EnablingConditions enablingConditions = EnablingConditions.valueOf(testplatfromHttpconfiguration.getHttpEbabling());
            testplatfromHttpconfiguration.setHttpEbabling(enablingConditions.getHttpEbablingMethod());
            if (enablingConditions.getHttpEbablingMethod().equals("ROUTE")){
                HttpRoutePatchMethod httpRoutePatchMethod = HttpRoutePatchMethod.valueOf(testplatfromHttpconfiguration.getHttpRoutePatch());
                testplatfromHttpconfiguration.setHttpRoutePatch(httpRoutePatchMethod.getHttpRoutePatchMethod());
            }
        }
        return testplatfromHttpconfigurations;
    }
}
